package teamCservlets;
import java.io.Serializable;
import java.sql.Time;
import java.text.SimpleDateFormat;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DefectBean implements java.io.Serializable {

	// Field for the defect class: all are private

	private int defectID;
	private int applicationID;
	private int submitterID;
	private int assigneeID;
	private String status;
	private String priority;
	private String summary;
	private String detailedDescription;
	private Time openDate;
	private String resolutionNote;
	private Time resolveDate;

	private SimpleDateFormat dateFormatter;

	// Two constructors.  No argument constructor sets an invalid
	// ID of -1.

	public DefectBean() {
		defectID = -1;
		applicationID = -1;
		submitterID = -1;
		assigneeID = -1;
		dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
	} // End of no arguments constructor with default ID (-1 is not valid)

	// Note: The openDate and resolveDate fields of this class are objects of class
	// java.sql.Time.  This class is very useful when interfacing with a MySQL database
	// but to make it easier work with date/time objects of this type, we're supplying
	// extra set and get methods that manipulate these as STRINGS.
	//
	// A date/time string should always have this format:
	//
	// YYYY-MM-DD HH:MM:SS in 24 hour time
	//
	// Example: 2014-08-31 14:00:00 means 2 PM on August 31, 2014.

	public void setStringOpenDate(String openDateString) {
		try	{
			this.openDate = new Time(dateFormatter.parse(openDateString).getTime());
		} catch (Exception e) {
		}
	}

	public void setStringResolveDate(String resolveDateString) {
		try {
			this.resolveDate = new Time(dateFormatter.parse(resolveDateString).getTime());
		} catch (Exception e) {
		}
	}

	public String getStringOpenDate() {
//		return((this.openDate != null) ? new String("'"+dateFormatter.format(this.openDate)+"'") : new String("NULL"));
		return((this.openDate != null) ? dateFormatter.format(this.openDate) : null);
	}

	public String getStringResolveDate() {
		return((this.resolveDate != null) ? new String("'"+dateFormatter.format(this.resolveDate)+"'") : new String("NULL"));
	}

	// Setter methods below:

	public void setDefectID(int inputID) {
		this.defectID = inputID;
	}

	public void setApplicationID(int inputAppID) {
		this.applicationID = inputAppID;
	}

	public void setSubmitterID(int inputSubID) {
		this.submitterID = inputSubID;
	}

	public void setAssigneeID(int inputAssignID) {
		if(inputAssignID != 0) {
			this.assigneeID = inputAssignID;
		} else {
			// Zero is not a valid person ID.  We use -1 to indicate
			// that the assignee field is invalid.
			this.assigneeID = -1;
		}
	}

	public void setStatus(String inStatus) {
		if((inStatus != null) && (!inStatus.equals("NULL"))) {
			this.status = new String(inStatus);
		} else {
			this.status = null;
		}
	}

	public void setPriority(String inPriority) {
		if(inPriority != null) {
			this.priority = new String(inPriority);
		} else {
			this.priority = null;
		}
	}

	public void setSummary(String inSummary) {
		if(inSummary != null && inSummary.length() > 0) {
			this.summary = new String(inSummary);
		} else {
			this.summary = null;
		}
	}

	public void setDetailedDescription(String description) {
		if(description != null && description.length() > 0) {
			this.detailedDescription = new String(description);
		} else {
			this.detailedDescription = null;
		}
	}

	public void setResolutionNote(String resNote) {
		if(resNote != null) {
			this.resolutionNote = new String(resNote);
		} else {
			this.resolutionNote = null;
		}
	}

	public void setOpenDate(Time inputDate) {
		if(inputDate != null) {
			this.openDate = new Time(inputDate.getTime());
		} else {
			this.openDate = null;
		}
	}

	public void setResolveDate(Time inputDate) {
		if(inputDate != null) {
			this.resolveDate = new Time(inputDate.getTime());
		} else {
			this.resolveDate = null;
		}
	}


	// Getter methods below:

	public int getDefectID() {
		return(this.defectID);
	}

	public int getApplicationID() {
		return(this.applicationID);
	}

	public int getSubmitterID() {
		return(this.submitterID);
	}

	public int getAssigneeID() {
		return(this.assigneeID);
	}

	public String getStatus() {
//		return((this.status != null) ? new String(this.status) : new String("NULL"));
		return this.status;
	}

	public String getPriority() {
//		return((this.priority != null) ? new String(this.priority) : new String("NULL"));
		return this.priority;
	}

	public String getSummary() {
//		 return((this.summary != null) ? new String(this.summary) : new String("NULL"));
		 return this.summary;
	}

	public String getDetailedDescription() {
//		 return((this.detailedDescription != null) ? new String(this.detailedDescription) : new String("NULL"));
		 return this.detailedDescription;
	}

	public String getStringResolutionNote() {
//		 return((this.resolutionNote != null) ? new String("'"+this.resolutionNote+"'") : new String("NULL"));
		 return this.resolutionNote;
	}

	public String getResolutionNote() {
//		 return((this.resolutionNote != null) ? new String(this.resolutionNote) : null);
		 return this.resolutionNote;
	}

	public Time getOpenDate() {
//		 return((this.openDate != null) ? new Time(this.openDate.getTime()) : null);
		 return this.openDate;
	}

	public Time getResolveDate() {
//		 return((this.resolveDate != null) ? new Time(this.resolveDate.getTime()) : null);
		 return this.resolveDate;
	}

	// Check if this defect is valid for storing as a new defect.  Return true or false.
	public boolean isValidNewDefect() {

		boolean isValid = false;
		
		if ((defectID == -1) &&
			(applicationID != -1) &&
			(submitterID != -1) &&
			(((assigneeID == -1) && (status.equals("NEW"))) ||
			((assigneeID != -1) && (status.equals("ASSIGNED")))) &&
			(priority != null) &&
			(summary != null) &&
			(detailedDescription != null) &&
			(openDate != null)) {
			isValid = true;
		}

		return isValid;
	}

} // End class DefectBean